package com.im.controller;

import javax.servlet.http.HttpServletRequest;
import javax.swing.plaf.synth.SynthEditorPaneUI;

import com.im.service.dto.PageQuery2;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.im.service.common.smo.ICommonSMO;
import com.im.service.contract.smo.IContractOperSMO;
import com.im.service.contract.smo.IContractQuerySMO;
import com.im.service.dto.BaseException;
import com.im.service.dto.Contract;
import com.im.service.dto.PageQuery;
import com.im.service.model.ContractAndItemVo;
import com.im.service.model.ContractProjectInfoVo;
import com.im.util.ReturnsUtil;
import com.im.util.StringUtil;

import net.sf.json.JSONObject;

/**
 * 合同管理后端服务
 * 
 * @ClassName: ContractService
 * @Description:
 * @author maoyj
 * @date 2018年4月3日
 *
 */
@RestController
@RequestMapping("/contractService")
public class ContractService {

	private static final Logger logger = LogManager.getLogger(ContractService.class);

	@Autowired
	private IContractQuerySMO contractQuerySMO;
	
	@Autowired
	private IContractOperSMO contractOperSMO;
	
	@Autowired
	ICommonSMO commonSMO;
	/**
	 * 
	 * 合同签订列表查询.
	 * 
	 * @param jsonStr
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryContractList")
	public String queryContractList(@RequestBody String jsonStr) {
		if (logger.isInfoEnabled()) {
			logger.info("请求入参json:{}", jsonStr);
		}
		
		try {
			if (jsonStr == null || jsonStr.length() <= 0) {
				throw new BaseException("请求入参为空，请核查数据!");
			}
			PageQuery2<Contract> result = contractQuerySMO.queryContractList(jsonStr);
			return ReturnsUtil.returnSuccess(result).toString();
		} catch (BaseException e) {
			if (logger.isErrorEnabled()) {
				logger.error(e.getStackTrace());
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}
	}

	/**
	 *
	 * 新增\修改合同签订
	 * 
	 * @param jsonStr
	 *            入参
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/saveOrUpdateContract")
	public String saveOrUpdateContract(@RequestBody String jsonStr) {
//		if (logger.isInfoEnabled()) {
//			logger.info("请求入参:{}", jsonStr);
//		}
		try {
			if (jsonStr == null) {
				throw new BaseException("请求入参为空，请核查数据!");
			}
			contractOperSMO.saveOrUpdateContract(jsonStr);
			return ReturnsUtil.returnSuccess(new JSONObject()).toString();
		} catch (BaseException e) {
			if (logger.isErrorEnabled()) {
				logger.error(e.getStackTrace());
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}
	}
	

	/**
	 * 
	 * 查询合同签订详情.
	 * 
	 * @param jsonStr
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryContractdetail")
	public String queryContractdetail(@RequestBody Contract contract) {
		if (logger.isInfoEnabled()) {
			logger.info("请求入参:{}", contract.toString());
		}

		try {
			if (contract == null || StringUtil.isEmpty(contract.getContractId())) {
				throw new BaseException("请求入参为空，请核查数据!");
			}
			Contract result = contractQuerySMO.queryContractdetail(contract);
			return ReturnsUtil.returnSuccess(result).toString();
		} catch (BaseException e) {
			if (logger.isErrorEnabled()) {
				logger.error(e.getStackTrace());
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}
	}
	
	
	/**
	 *
	 * 删除合同签订信息
	 * 
	 * @param planProject
	 *            入参
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/deleteContract")
	public String deleteContract(@RequestBody String jsonStr) {
		if (logger.isInfoEnabled()) {
			logger.info("请求入参:{}", jsonStr);
		}
		try {
			contractOperSMO.deleteContract(jsonStr);
			return ReturnsUtil.returnSuccess(new JSONObject()).toString();
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}
	}

	/**
	 * 
	 * 合同分项列表查询.
	 * 
	 * @param jsonStr
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryContractItemList")
	public String queryContractItemList(@RequestBody String jsonStr) {
		if (logger.isInfoEnabled()) {
			logger.info("请求入参json:{}", jsonStr);
		}

		try {
			if (jsonStr == null || jsonStr.length() <= 0) {
				throw new BaseException("请求入参为空，请核查数据!");
			}
			PageQuery<ContractAndItemVo> result = contractQuerySMO.queryContractItemList(jsonStr);

			return ReturnsUtil.returnSuccess(result).toString();
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}
	}

	/**
	 *
	 * 新增\修改合同分项
	 * 
	 * @param jsonStr
	 *            入参
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/saveOrUpdateContractItem")
	public String saveOrUpdateContractItem(@RequestBody String jsonStr) {
		if (logger.isInfoEnabled()) {
			logger.info("请求入参:{}", jsonStr);
		}
		try {
			if (jsonStr == null) {
				throw new BaseException("请求入参为空，请核查数据!");
			}
			contractOperSMO.saveOrUpdateContractItem(jsonStr);
			return ReturnsUtil.returnSuccess(new JSONObject()).toString();
		} catch (BaseException e) {
			if (logger.isErrorEnabled()) {
				logger.error(e.getStackTrace());
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}
	}

	/**
	 *
	 * 新增\修改合同分项变更
	 * 
	 * @param jsonStr
	 *            入参
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/saveOrUpdateContractIteChange")
	public String saveOrUpdateContractIteChange(@RequestBody String jsonStr) {
		if (logger.isInfoEnabled()) {
			logger.info("请求入参:{}", jsonStr);
		}
		try {
			if (jsonStr == null) {
				throw new BaseException("请求入参为空，请核查数据!");
			}
			contractOperSMO.saveOrUpdateContractIteChange(jsonStr);
			return ReturnsUtil.returnSuccess(new JSONObject()).toString();
		} catch (BaseException e) {
			if (logger.isErrorEnabled()) {
				logger.error(e.getStackTrace());
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}
	}

	/**
	 * 
	 * 合同签订分项查询.
	 * 
	 * @param jsonStr
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryContractItemDetail")
	public String queryContractItemDetail(@RequestBody String jsonStr) {
		if (logger.isInfoEnabled()) {
			logger.info("请求入参json:{}", jsonStr);
		}
		try {
			ContractAndItemVo result = contractQuerySMO.queryContractItemDetail(jsonStr);
			return ReturnsUtil.returnSuccess(result).toString();
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}
	}
	
	/**
	 *
	 * excel导入合同分项
	 * 
	 * @param jsonStr
	 *            入参
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/importContractItem", method = RequestMethod.POST)
	@ResponseBody
	public String importContractItem(HttpServletRequest request,
			@RequestParam(value = "myFile", required = false) MultipartFile file) {
		if (logger.isInfoEnabled()) {
			logger.info("请求入参:{}", file.getOriginalFilename());
		}
		try {
			String contractClauseId = request.getParameter("contractClauseId");
			String changeexport = request.getParameter("changeexport");
			contractOperSMO.importContractItem(file, contractClauseId, changeexport);
			return ReturnsUtil.returnSuccess(new JSONObject()).toString();
		} catch (BaseException e) {
			if (logger.isErrorEnabled()) {
				logger.error(e.getStackTrace());
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}
	}

	/**
	 *
	 * 删除合同分项信息
	 * 
	 * @param planProject
	 *            入参
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/deleteContractItem")
	public String deleteContractItem(@RequestBody String jsonStr) {
		if (logger.isInfoEnabled()) {
			logger.info("请求入参:{}", jsonStr);
		}
		try {
			contractOperSMO.deleteContractItem(jsonStr);
			return ReturnsUtil.returnSuccess(new JSONObject()).toString();
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}
	}

	/**
	 *
	 * 删除合同分项变更
	 * 
	 * @param planProject
	 *            入参
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/deleteContractItemChange")
	public String deleteContractItemChange(@RequestBody String jsonStr) {
		if (logger.isInfoEnabled()) {
			logger.info("请求入参:{}", jsonStr);
		}
		try {
			contractOperSMO.deleteContractItemChange(jsonStr);
			return ReturnsUtil.returnSuccess(new JSONObject()).toString();
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}
	}

	/**
	 *
	 * 审定合同分项信息
	 * 
	 * @param planProject
	 *            入参
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/examineContractItem")
	public String examineContractItem(@RequestBody String jsonStr) {
		if (logger.isInfoEnabled()) {
			logger.info("请求入参:{}", jsonStr);
		}
		try {
			contractOperSMO.examineContractItem(jsonStr);
			return ReturnsUtil.returnSuccess(new JSONObject()).toString();
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}
	}

	/**
	 *
	 * 审定合同分项变更信息
	 * 
	 * @param planProject
	 *            入参
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/examineContractItemChange")
	public String examineContractItemChange(@RequestBody String jsonStr) {
		if (logger.isInfoEnabled()) {
			logger.info("请求入参:{}", jsonStr);
		}
		try {
			contractOperSMO.examineContractItemChange(jsonStr);
			return ReturnsUtil.returnSuccess(new JSONObject()).toString();
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}
	}

	/**
	 * 
	 * 合同分项变更列表查询.
	 * 
	 * @param jsonStr
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryCanChangeContractItemList")
	public String queryCanChangeContractItemList(@RequestBody String jsonStr) {
		if (logger.isInfoEnabled()) {
			logger.info("请求入参json:{}", jsonStr);
		}

		try {
			if (jsonStr == null || jsonStr.length() <= 0) {
				throw new BaseException("请求入参为空，请核查数据!");
			}
			PageQuery<ContractAndItemVo> result = contractQuerySMO.queryCanChangeContractItemChangeList(jsonStr);

			return ReturnsUtil.returnSuccess(result).toString();
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}
	}

	/**
	 * 
	 * 合同分项变更审批列表查询
	 * 
	 * @param jsonStr
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryChangeExamineList")
	public String queryChangeExamineList(@RequestBody String jsonStr) {
		if (logger.isInfoEnabled()) {
			logger.info("请求入参json:{}", jsonStr);
		}

		try {
			if (jsonStr == null || jsonStr.length() <= 0) {
				throw new BaseException("请求入参为空，请核查数据!");
			}
			PageQuery<ContractAndItemVo> result = contractQuerySMO.queryChangeExamineList(jsonStr);

			return ReturnsUtil.returnSuccess(result).toString();
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}
	}

	/**
	 * 
	 * 通过项目id查询中标通知书、采购计划信息
	 * 
	 * @param jsonStr
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryNoticeAndPurchasePlanByProjectId")
	public String queryNoticeAndPurchasePlanByProjectId(@RequestBody String jsonStr) {
		if (logger.isInfoEnabled()) {
			logger.info("请求入参json:{}", jsonStr);
		}

		try {
			if (jsonStr == null || jsonStr.length() <= 0) {
				throw new BaseException("请求入参为空，请核查数据!");
			}
			ContractProjectInfoVo result = contractQuerySMO.queryNoticeAndPurchasePlanByProjectId(jsonStr);
			if (result == null) {
				return ReturnsUtil.returnSuccess(new JSONObject()).toString();
			}
			String contractcode = commonSMO.querySeqForRule("4", result.getImpleProjCode());
			result.setContractCode(contractcode);;
			return ReturnsUtil.returnSuccess(result).toString();
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}
	}

	/**
	 *
	 * 通过采购计划查询中标信息
	 *
	 * @param jsonStr
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryNoticeByPurchPlanId")
	public String queryNoticeByPurchPlanId(@RequestBody String jsonStr) {
		if (logger.isInfoEnabled()) {
			logger.info("请求入参json:{}", jsonStr);
		}

		try {
			if (jsonStr == null || jsonStr.length() <= 0) {
				throw new BaseException("请求入参为空，请核查数据!");
			}
			ContractProjectInfoVo result = contractQuerySMO.queryNoticeByPurchPlanId(jsonStr);
			if (result == null) {
				return ReturnsUtil.returnSuccess(new JSONObject()).toString();
			}
			return ReturnsUtil.returnSuccess(result).toString();
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}
	}

	/**
	 *
	 * 合同签订批量审定
	 *
	 * @param jsonStr
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/updateBatchContract")
	public String updateBatchContract(@RequestBody String jsonStr) {
		if (logger.isInfoEnabled()) {
			logger.info("请求入参json:{}", jsonStr);
		}

		try {
			if (jsonStr == null || jsonStr.length() <= 0) {
				throw new BaseException("请求入参为空，请核查数据!");
			}
			Object result = contractOperSMO.updateBatchContract(jsonStr);
			if (result == null) {
				return ReturnsUtil.returnSuccess(new JSONObject()).toString();
			}
			return ReturnsUtil.returnSuccess(result).toString();
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}
	}
}
